package processor

import (
	"chat-room/common"
	"chat-room/server/functions"
	"chat-room/server/global"
	"chat-room/server/model"
	"encoding/json"
	"fmt"
	"net"
)

type User struct {
	Conn net.Conn
}

//登录
func (that *User) Login (account string, password string){
	//返回的消息结构体
	loginResponseMsgEx := &common.LoginResponseMsg{
		Code: 0,
		Msg:  "登录成功",
		Name: "",
	}
	//根据账号查询用户信息
	userEx := &model.User{}
	err := userEx.GetUserInfoByAccount(account)
	if err != nil {
		fmt.Printf("userEx.GetUserInfoByAccount:%v\n",err)
		loginResponseMsgEx.Code = common.SystemErrCode
		loginResponseMsgEx.Msg  = common.SystemErrMsg
	} else if userEx.Id == 0 {
		//未查询到账号
		loginResponseMsgEx.Code = common.AccountNotFindErrCode
		loginResponseMsgEx.Msg  = common.AccountNotFindErrMsg
	} else if userEx.Password != password{
		//密码错误
		loginResponseMsgEx.Code = common.PassWordIsWrongErrCode
		loginResponseMsgEx.Msg  = common.PassWordIsWrongErrMsg
	} else {
		loginResponseMsgEx.Name = userEx.Name
	}
	//转成json切片
	loginResponseMsgExByte, _ := json.Marshal(loginResponseMsgEx)
	//传递消息结构体
	messageEx := &common.Message{
		Type: common.LoginResponseMsgType,
		Data: string(loginResponseMsgExByte),
	}
	//转成json切片
	messageExByte, _ := json.Marshal(messageEx)
	//返回信息
	functionsEx := &common.Functions{}
	err = functionsEx.SendMsg(that.Conn, messageExByte)
	if err != nil {
		fmt.Printf("functionsEx.SendMsg:%v\n",err)
	}
	//把连接加入 global.AppendLoginMap
	serverFunctionsEx := &functions.Functions{}
	serverFunctionsEx.AppendLoginMap(that.Conn, loginResponseMsgEx.Name)
	//给所有客户端发送新用户上线消息
	sendStr := fmt.Sprintf("%v上线啦",loginResponseMsgEx.Name)
	messageExByte, err = serverFunctionsEx.MakeSystemMsgStruct(sendStr)
	global.AllLoginMap.Range(func(key, value interface{}) bool {
		if key != that.Conn {
			err := functionsEx.SendMsg(key.(net.Conn), messageExByte)
			if err != nil {
				fmt.Printf("functionsEx.SendMsg:%v\n",err)
			}
		}
		return true
	})
}

//注册
func (that *User) Register(account string, password string, name string){
	//返回的消息结构体
	registerResponseMsgEx := &common.RegisterResponseMsg{
		Code: 0,
		Msg:  "注册成功",
		Name: "",
	}
	//根据账号查询用户信息
	userEx := &model.User{}
	err := userEx.GetUserInfoByAccount(account)
	if err != nil {
		fmt.Printf("userEx.GetUserInfoByAccount:%v\n",err)
		registerResponseMsgEx.Code = common.SystemErrCode
		registerResponseMsgEx.Msg  = common.SystemErrMsg
	} else if userEx.Id != 0 {
		registerResponseMsgEx.Code = common.AccountIsRegisteredErrCode
		registerResponseMsgEx.Msg  = common.AccountIsRegisteredErrMsg
	} else {
		err = userEx.AddUser(name, account, password)
		if err != nil {
			fmt.Printf("userEx.AddUser:%v\n",err)
			registerResponseMsgEx.Code = common.SystemErrCode
			registerResponseMsgEx.Msg  = common.SystemErrMsg
		}else{
			registerResponseMsgEx.Name = name
		}
	}
	//转成json切片
	registerResponseMsgByte, _ := json.Marshal(registerResponseMsgEx)
	//传递消息结构体
	messageEx := &common.Message{
		Type: common.RegisterResponseMsgType,
		Data: string(registerResponseMsgByte),
	}
	//转成json切片
	messageExByte, _ := json.Marshal(messageEx)
	//返回信息
	functionsEx := &common.Functions{}
	err = functionsEx.SendMsg(that.Conn, messageExByte)
	if err != nil {
		fmt.Printf("functionsEx.SendMsg:%v\n",err)
	}
}

//获取在线用户列表
func (that *User) GetOnlineUsers(){
	var AllLoginSlice []string
	global.AllLoginMap.Range(func(key, value interface{}) bool {
		AllLoginSlice = append(AllLoginSlice, value.(string))
		return true
	})
	//转成json切片
	AllLoginMapByte, _ := json.Marshal(AllLoginSlice)
	//传递消息结构体
	messageEx := &common.Message{
		Type: common.GetOnlineUsersResponseMsgType,
		Data: string(AllLoginMapByte),
	}
	//转成json切片
	messageExByte, _ := json.Marshal(messageEx)
	//返回信息
	functionsEx := &common.Functions{}
	err := functionsEx.SendMsg(that.Conn, messageExByte)
	if err != nil {
		fmt.Printf("functionsEx.SendMsg:%v\n",err)
	}
}